package com.laker.admin.module.datasource.facade.impl;

import com.laker.admin.framework.constants.CodeTable;
import com.laker.admin.framework.exception.BusinessRuntimeException;
import com.laker.admin.framework.model.Response;
import com.laker.admin.module.datasource.entity.DataSourceCmd;
import com.laker.admin.module.datasource.entity.dto.DsTableDto;
import com.laker.admin.module.datasource.facade.DataSourceFacade;
import com.laker.admin.module.datasource.util.MssqlUtil;
import com.laker.admin.module.datasource.util.MysqlUtil;
import com.laker.admin.module.excel.application.DataSourceApplication;
import com.laker.admin.module.excel.domain.DataSource;
import com.laker.admin.module.excel.entity.ExcelFillCmd;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author : nihongxin
 * @date : 2021/12/08
 */
@Slf4j
@Service
public class DataSourceFacadeImpl implements DataSourceFacade {

    @Autowired
    private DataSourceApplication dataSourceApplication;

    @Override
    public Response checkConnection(DataSourceCmd dataSourceCmd) {
        try {
            switch (dataSourceCmd.getType()) {
                case CodeTable.SOURCE_TYPE_MYSQL:
                    return MysqlUtil.checkConnection(dataSourceCmd.getDataSourceUrl(), dataSourceCmd.getUsername(), dataSourceCmd.getPassword(), CodeTable.MYSQL_DRIVER);
                case CodeTable.SOURCE_TYPE_SQLSERVER:
                    return MssqlUtil.checkConnection(dataSourceCmd.getDataSourceUrl(), dataSourceCmd.getUsername(), dataSourceCmd.getPassword());
                default:
                    throw new BusinessRuntimeException(CodeTable.PARAM_ERR, "type值无效: " + dataSourceCmd.getType() + "，目录只支持mysql类型的数据源！");
            }
        } catch (BusinessRuntimeException e) {
            return Response.error(String.valueOf(e.getCode()), e.getMessage());
        }
    }

    @Override
    public Response getDataBaseList(DataSourceCmd dataSourceCmd) {
        try {
            switch (dataSourceCmd.getType()) {
                case CodeTable.SOURCE_TYPE_MYSQL:
                    return MysqlUtil.getDataBaseList(dataSourceCmd.getDataSourceUrl(), dataSourceCmd.getUsername(), dataSourceCmd.getPassword(), CodeTable.MYSQL_DRIVER);
                case CodeTable.SOURCE_TYPE_SQLSERVER:
                    return MssqlUtil.getDataBaseList(dataSourceCmd.getDataSourceUrl(), dataSourceCmd.getUsername(), dataSourceCmd.getPassword());
                default:
                    throw new BusinessRuntimeException(CodeTable.PARAM_ERR, "type值无效: " + dataSourceCmd.getType() + "，目录只支持mysql类型的数据源！");
            }
        } catch (BusinessRuntimeException e) {
            return Response.error(String.valueOf(e.getCode()), e.getMessage());
        }
    }

    @Override
    public Response getTableList(DataSourceCmd dataSourceCmd) {
        try {
            switch (dataSourceCmd.getType()) {
                case CodeTable.SOURCE_TYPE_MYSQL:
                    List<DsTableDto> dsTableDtoList = MysqlUtil.queryTableList(dataSourceCmd.getDataSourceUrl(), dataSourceCmd.getUsername(), dataSourceCmd.getPassword(), CodeTable.MYSQL_DRIVER, dataSourceCmd.getDatabaseName());
                    return Response.ok(dsTableDtoList);
                case CodeTable.SOURCE_TYPE_SQLSERVER:
                    List<DsTableDto> dsTableList = MssqlUtil.queryTableList(dataSourceCmd.getDataSourceUrl(), dataSourceCmd.getUsername(), dataSourceCmd.getPassword(), dataSourceCmd.getDatabaseName());
                    return Response.ok(dsTableList);
                default:
                    throw new BusinessRuntimeException(CodeTable.PARAM_ERR, "type值无效: " + dataSourceCmd.getType() + "，目录只支持mysql类型的数据源！");
            }
        } catch (BusinessRuntimeException e) {
            return Response.error(String.valueOf(e.getCode()), e.getMessage());
        }
    }

    @Override
    public Response getTableDetail(DataSourceCmd dataSourceCmd) {
        try {
            switch (dataSourceCmd.getType()) {
                case CodeTable.SOURCE_TYPE_MYSQL:
                    return MysqlUtil.queryTableDetailWithIgnore(dataSourceCmd.getDataSourceUrl(), dataSourceCmd.getUsername(), dataSourceCmd.getPassword(), CodeTable.MYSQL_DRIVER, dataSourceCmd.getDatabaseName(), dataSourceCmd.getTableName());
                case CodeTable.SOURCE_TYPE_SQLSERVER:
                    return MssqlUtil.queryTableDetailWithIgnore(dataSourceCmd.getDataSourceUrl(), dataSourceCmd.getUsername(), dataSourceCmd.getPassword(), dataSourceCmd.getDatabaseName(), dataSourceCmd.getTableName());
                default:
                    throw new BusinessRuntimeException(CodeTable.PARAM_ERR, "type值无效: " + dataSourceCmd.getType() + "，目录只支持mysql类型的数据源！");
            }
        } catch (BusinessRuntimeException e) {
            return Response.error(String.valueOf(e.getCode()), e.getMessage());
        }
    }

    @Override
    public Response getTableData(DataSourceCmd dataSourceCmd) {
        try {
            switch (dataSourceCmd.getType()) {
                case CodeTable.SOURCE_TYPE_MYSQL:
                    return MysqlUtil.queryTableData(dataSourceCmd.getDataSourceUrl(), dataSourceCmd.getUsername(), dataSourceCmd.getPassword(), CodeTable.MYSQL_DRIVER, dataSourceCmd.getDatabaseName(), dataSourceCmd.getTableName(), dataSourceCmd.getFieldNameList());
                case CodeTable.SOURCE_TYPE_SQLSERVER:
                    return MssqlUtil.queryTableData(dataSourceCmd.getDataSourceUrl(), dataSourceCmd.getUsername(), dataSourceCmd.getPassword(), dataSourceCmd.getDatabaseName(), dataSourceCmd.getTableName(), dataSourceCmd.getFieldNameList());
                default:
                    throw new BusinessRuntimeException(CodeTable.PARAM_ERR, "type值无效: " + dataSourceCmd.getType() + "，目录只支持mysql类型的数据源！");
            }
        } catch (BusinessRuntimeException e) {
            return Response.error(String.valueOf(e.getCode()), e.getMessage());
        }
    }

    @Override
    public Response updateTableData(DataSourceCmd dataSourceCmd) {
        try {
            switch (dataSourceCmd.getType()) {
                case CodeTable.SOURCE_TYPE_MYSQL:
                    return MysqlUtil.updateTableData(dataSourceCmd.getDataSourceUrl(), dataSourceCmd.getUsername(), dataSourceCmd.getPassword(), CodeTable.MYSQL_DRIVER, dataSourceCmd.getDatabaseName(), dataSourceCmd.getTableName(), dataSourceCmd.getFieldName(), dataSourceCmd.getFieldValue(), dataSourceCmd.getId());
                case CodeTable.SOURCE_TYPE_SQLSERVER:
                    return MssqlUtil.updateTableData(dataSourceCmd.getDataSourceUrl(), dataSourceCmd.getUsername(), dataSourceCmd.getPassword(), dataSourceCmd.getDatabaseName(), dataSourceCmd.getTableName(), dataSourceCmd.getFieldName(), dataSourceCmd.getFieldValue(), dataSourceCmd.getId());
                default:
                    throw new BusinessRuntimeException(CodeTable.PARAM_ERR, "type值无效: " + dataSourceCmd.getType() + "，目录只支持mysql类型的数据源！");
            }
        } catch (BusinessRuntimeException e) {
            return Response.error(String.valueOf(e.getCode()), e.getMessage());
        }
    }

    @Override
    public Response save(DataSource dataSource) {
        try {
            if (CodeTable.SOURCE_TYPE_MYSQL == dataSource.getType()) {
                dataSource.setDataSourceDriver(CodeTable.MYSQL_DRIVER);
            } else {
                dataSource.setDataSourceDriver(CodeTable.MSSQL_DRIVER);
            }
            return Response.ok(dataSourceApplication.save(dataSource));
        } catch (Exception e) {
            return Response.error(e.getMessage());
        }
    }

    @Override
    public Response list(ExcelFillCmd excelFillCmd) {
        try {
            Page<DataSource> dataSourcePage = dataSourceApplication.listDataSourcePage(excelFillCmd.getKeyword(), excelFillCmd.getPageNo(), excelFillCmd.getPageSize());
            return Response.ok(dataSourcePage);
        } catch (Exception e) {
            return Response.error(e.getMessage());
        }
    }

    @Override
    public Response getById(Long id) {
        try {
            return Response.ok(dataSourceApplication.getById(id));
        } catch (Exception e) {
            return Response.error(e.getMessage());
        }
    }

    @Override
    public Response deleteById(Long id) {
        try {
            dataSourceApplication.deleteById(id);
            return Response.ok("删除成功");
        } catch (Exception e) {
            return Response.error(e.getMessage());
        }
    }
}
